package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataObject.ActivityDO;
import com.example.lotterysystem.dao.dataObject.ActivityPrizeDO;
import com.example.lotterysystem.dao.dataObject.ActivityUserDO;
import com.example.lotterysystem.service.enums.ActivityStatusEnum;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ActivityMapper {
    @Insert("insert into activity(activity_name, description, status)" +
            " values(#{activityName}, #{description}, #{status})")
    @Options(useGeneratedKeys = true,keyProperty ="id",keyColumn = "id")
    int insert(ActivityDO activityDO);

    @Select("select count(1) from activity")
    int count();

    @Select("select * from activity order by id desc limit #{offset},#{pageSize}")
    List<ActivityDO> selectActivityList(@Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    @Select("select * from activity where id=#{id}")
    ActivityDO selectById(@Param("id") Long activityId);

    @Update("update activity set status=#{status} where id = #{id}")
    void updateStatus(@Param("id") Long activityId, @Param("status") ActivityStatusEnum Status);

    @Select("<script>" +
            "SELECT COUNT(1) FROM activity WHERE status = #{status}" +
            " <if test='items != null and items.size() > 0'>" +
            " AND id IN" +
            " <foreach collection='items' item='item' separator=',' open='(' close=')'>" +
            " #{item.activityId}" +
            " </foreach>" +
            " </if>" +
            " </script>")
    int countActivityInitByPrize(@Param("items") List<ActivityPrizeDO> activityPrizeDOS, @Param("status") String status);

    @Select("<script>" +
            "SELECT COUNT(1) FROM activity WHERE status = #{status}" +
            " <if test='items != null and items.size() > 0'>" +
            " AND id IN" +
            " <foreach collection='items' item='item' separator=',' open='(' close=')'>" +
            " #{item.activityId}" +
            " </foreach>" +
            " </if>" +
            " </script>")
    int countActivityInitByUser(@Param("items")List<ActivityUserDO> activityUserDOS, @Param("status") String status);
}
